package pages;

import core.TestBase;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import utils.Log4Test;

import java.util.concurrent.TimeUnit;

public class NotebooksApplePage extends TestBase {

    protected By sortItem = By.xpath("//*[@id='sort_view']/a");
    protected By sortExpensiveCheap = By.xpath("//a[@class='lightblue'][@href='http://rozetka.com.ua/notebooks/c80004/filter/producer=apple;sort=expansiv/']");
    protected By AppleMacBookZ0PU002JE = By.xpath("//div[@class='gtile-i-title']/a[@href='http://rozetka.com.ua/apple_macbook_pro_retina_15_z0pu002je/p699984/']");
    protected By AppleMacBookCompare = By.xpath("//*[@class='list-compare']/*/*[@class='block']");
    protected By AppleMacBookZ0PU002JESelect = By.xpath("//div[@class='gtile-i-title']/a[@href='http://rozetka.com.ua/apple_macbook_pro_retina_15_z0pu002je/p699984/']/../..//input[@class='g-tools-to-compare-check']");
    protected By AppleMacBookME294UAA = By.xpath("//div[@class='gtile-i-title']/a[@href='http://rozetka.com.ua/apple_macbook_pro_retina_15_me294ua_a/p338650/']");
    protected By AppleMacBookME294UAASelect = By.xpath("//div[@class='gtile-i-title']/a[@href='http://rozetka.com.ua/apple_macbook_pro_retina_15_me294ua_a/p338650/']/../..//input[@class='g-tools-to-compare-check']");
    protected By CompareItem = By.xpath("//div[@class='gtile-i-title']/a[@href='http://rozetka.com.ua/apple_macbook_pro_retina_15_z0pu002je/p699984/']/../..//a[@class='lightblue underline']");
    private String URL = "http://rozetka.com.ua/notebooks/apple/c80004/v069/";

    public boolean isOpened() {
        return webDriver.getCurrentUrl().equals(URL);
    }

    public void clickSort() {
        Log4Test.info("Click sort list");
        Element = webDriver.findElement(sortItem);
        Element.click();
    }

    public void clickSortExpensCheap() {
        clickSort();
        Log4Test.info("Click sort from expensive to cheaper");
        Element = webDriver.findElement(sortExpensiveCheap);
        Element.click();
    }


    public WebElement verifyAppleMacBookZ0PU002JE() {
        Log4Test.info("verify Apple Mac Book ProRetina 15 Z0PU002JE");
        return Element = webDriver.findElement(AppleMacBookZ0PU002JE);
    }

    public WebElement testSelectZ0PU002JEdVerify() {
        Element = webDriver.findElement(AppleMacBookZ0PU002JE);
        if (!Element.getText().toString().contains("checked")) {
            Element = webDriver.findElement(AppleMacBookZ0PU002JESelect);
            Element.click();
            Log4Test.info("Apple Mac Book ProRetina 15 Z0PU002JE selected");
        }
        Log4Test.info("Apple Mac Book ProRetina 15 Z0PU002JE and verify link for compare");
        return Element = webDriver.findElement(AppleMacBookCompare);
    }

    public WebElement getZ0PU002JE() {
        return Element = webDriver.findElement(AppleMacBookCompare);
    }

    public WebElement verifyAppleMacBookME294UAA() {
        Log4Test.info("verify Apple Mac Book ProRetina 15 ME294UA/A on the page");
        return Element = webDriver.findElement(AppleMacBookME294UAA);
    }

    public String testSelectME294UAAVerify() {
        Element = webDriver.findElement(AppleMacBookME294UAA);

        if (!Element.getText().toString().contains("checked")) {
            Element = webDriver.findElement(AppleMacBookME294UAASelect);
            Log4Test.info("checked");
            Element.click();

            Log4Test.info("Apple Mac Book ProRetina 15 ME294UAA selected link");
        }
        Log4Test.info("Apple Mac Book ProRetina 15 ME294UAA and verify link for compare");

        Element = webDriver.findElement(AppleMacBookCompare);

        Log4Test.info(Element.getText().toString());
        Element = webDriver.findElement(AppleMacBookCompare);
        webDriver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

        return Element.getText().toString();
    }

    public void ClickLinkInCompareItem() {
        Element = webDriver.findElement(CompareItem);
        Element.click();
        Log4Test.info("Clicked link В сравнении");
    }
}
